// components/chart-bar.js
import * as echarts from '../ec-canvas/echarts';
// 柱状图
let chartBar;
function getBarOption(data) {
  let option = {
    title: {
      text: '学生报修类型统计',
      left: 'center'
    },
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      }
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: [{
      type: 'category',
      data: ['空调', '网络', '热水器', '宿舍设施', '其他'],
      axisTick: {
        alignWithLabel: true
      }
    }],
    yAxis: [{
      type: 'value'
    }],
    series: [{
      name: '数量',
      type: 'bar',
      barWidth: '60%',
      data: data,
      label: {
        show: true,
        position: 'top'
      },
    }]
  };
  return option
}
function initChartBar(canvas, width, height, dpr) {
  chartBar = echarts.init(canvas, null, {
    width: width,
    height: height,
    devicePixelRatio: dpr // new
  });
  canvas.setChart(chartBar);
  let option = getBarOption();
  chartBar.setOption(option);
  return chartBar;
}
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    oData:{
      type:Array,
      value:[],
      observer: function (newVal, oldVal, changedPath) {
        if (newVal) {
          setTimeout(()=>{
            chartBar.setOption({
              series: [{
                data: newVal
              }]
            });
          },500)
        }
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    ec: {
      onInit: initChartBar, // 3、将数据放入到里面
    }
  },
  attached(){
  },
  /**
   * 组件的方法列表
   */
  methods: {

  }
})
